package com.jkb.trainjieba;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.HashSet;
import java.util.List;

import org.ansj.domain.Term;

import org.ansj.splitWord.analysis.ToAnalysis;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.huaban.analysis.jieba.JiebaSegmenter.SegMode;

/**
 * 
 * @author wangbin
 *
 * 功能实现：对要训练的数据进行分词
 * 注:使用jieba分词器
 * 输入：文章类别\t\t\t正文\t\t\turl
 * 输出：词\t\t\t文章类别\t\t\turl
 * 运行步骤：第一步
 */
public class SplitWordsMR {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//args0:dst
		//args1:out
		//args2:splitMB
		//args3:hdfs 停用词路径
		if(args.length != 4){
			System.out.println("args0:dst");
			System.out.println("args1:out");
			System.out.println("args2:splitMB");
			System.out.println("args3:hdfs 停用词路径");
			System.exit(0);
		}
		int SplitMB = Integer.valueOf(args[2]);
		String dst = args[0];
		String out = args[1];
		Configuration conf = new Configuration();
		conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.min.split.size", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(SplitMB * 1024 * 1024));
		Job job =  Job.getInstance(conf);
		FileInputFormat.addInputPath(job, new Path(dst));
		FileOutputFormat.setOutputPath(job, new Path(out));
		
		job.addCacheFile(new Path(args[3]).toUri());
		job.addFileToClassPath(new Path("/edu/jarpackage/jieba-analysis-1.0.0.jar"));
		job.setMapperClass(FCMap.class);
		
		job.setNumReduceTasks(0);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		
		job.setJarByClass(SplitWordsMR.class);
        job.waitForCompletion(true);
		
	}
	
	public static class FCMap extends Mapper<Object,Text,Text,Text>{
		
		public final static String SP = "\t\t\t";
		
		//停用词容器
		HashSet<String> stopwords = new HashSet<String>();
		
		@Override
		public void setup(Context context) throws IOException{
			//Path[] paths = context.getLocalCacheFiles();
			URI[] paths = context.getCacheFiles();
			//加载停用词
			BufferedReader stopreader = new BufferedReader(new FileReader(paths[0].toString()));
			String stopline;
			while((stopline = stopreader.readLine()) != null){
				stopwords.add(stopline);
			}
		}
		
		@Override
		public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
			String[] line = value.toString().split(SP);
			if(line.length >= 3){
				String type = line[0];
				String article = line[1];
				String url = line[2];
				JiebaSegmenter Segmenter = new JiebaSegmenter();
				List<SegToken> STList = Segmenter.process(article.toString(), SegMode.SEARCH);
				for(SegToken st : STList){
					try{
						String wd = st.word; //getToken();
						if(stopwords.contains(wd)) continue;
						String k = wd + SP + type + "\t\t";
						context.write(new Text(k),new Text(url));
					}catch(Exception e){
						System.out.println(e.toString());
					}
					
				}
			}
		}
	}

}
